//
//  HomeHeaderView.m
//  CarOfHome2
//
//  Created by dlios on 15/9/23.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "HomeHeaderView.h"
#import "STYHomeSliderModel.h"



@interface HomeHeaderView ()<UIScrollViewDelegate,UIPageViewControllerDelegate>

@property (retain, nonatomic) UIScrollView *scrollView;

@property (retain, nonatomic) UIPageControl *pageView;


@property (retain, nonatomic) NSMutableArray *photoListArr;

@property (nonatomic, retain) NSTimer *timer;

@end



@implementation HomeHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _scrollView = [[UIScrollView alloc] initWithFrame:frame];
        _pageView = [[UIPageControl alloc] initWithFrame:CGRectMake((VWidth - 200) / 2 * SAWindth, 150 * SAHeight, 200 * SAWindth, 40 * SAHeight)];
        
        [self addSubview:_scrollView];
        [self addSubview:_pageView];
        

        self.timer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(autoSlide) userInfo:nil repeats:YES];
        /* 创建 消息循环 */
        NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
        /* 通过消息循环 把计时器的优先级提高到 与点击事件 一样 高*/
        [runLoop addTimer:self.timer forMode:NSRunLoopCommonModes];
        
        //#pragma mark - tap
        //    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        //    tap.numberOfTapsRequired = 1;
        //    tap.numberOfTouchesRequired = 1;
        //    [self.scrollView addGestureRecognizer:tap];
        
    }
    return self;
}

- (void)setSliderArr:(NSArray *)sliderArr
{
    if (_sliderArr != sliderArr) {
        _sliderArr = sliderArr;
    }
    
    self.pageView.numberOfPages = self.sliderArr.count;
    
    [self addSubview:self.pageView];
    
    
    if (self.sliderArr.count != 0) {
        self.photoListArr = [NSMutableArray arrayWithArray:self.sliderArr];
        STYHomeSliderModel *firstModel = [self.sliderArr objectAtIndex:self.sliderArr.count - 1];
        STYHomeSliderModel *lastModel = [self.sliderArr objectAtIndex:0];
        [self.photoListArr addObject:lastModel];
        [self.photoListArr insertObject:firstModel atIndex:0];
    }
    
    self.scrollView.contentSize = CGSizeMake(VWidth * self.photoListArr.count, VHeight);
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.backgroundColor = [UIColor whiteColor];
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.bounces = YES;
    
    int i = 0;
    
    for (STYHomeSliderModel *hotSlider in self.photoListArr) {
        UIImageView *SliderimageView = [[UIImageView alloc] initWithFrame:CGRectMake(VWidth * i , 0, VWidth , VHeight )];
        [SliderimageView sd_setImageWithURL:[NSURL URLWithString:hotSlider.imgURL] placeholderImage:[UIImage imageNamed:@""]];
        SliderimageView.userInteractionEnabled = YES;
        
        
        [self.scrollView addSubview:SliderimageView];
        
        
        i++;
    }
    
    [self.scrollView setContentOffset:CGPointMake(VWidth , 0) animated:NO];
    
    [self.pageView addTarget:self action:@selector(pageChange) forControlEvents:UIControlEventValueChanged];


    
}


- (void)autoSlide
{
    /* 通过页数判断是否是最后一张图片*/
    if (self.pageView.currentPage < [self.photoListArr count] - 3) {
        /* 如果不是 页码自增1 */
        self.pageView.currentPage += 1;
        /* 调用pageChange */
        [self pageChange];
        
    }else  {
        
        /* 如果是 在往后移动 一张 图片 */
        [_scrollView setContentOffset:CGPointMake(VWidth * (self.pageView.currentPage + 1), 0) animated:true];
        
        self.pageView.currentPage = 0;
        //        [_scrollView setContentOffset:CGPointMake(Width, 0) animated:false];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [_scrollView setContentOffset:CGPointMake(VWidth, 0) animated:false];
        });
        
        
    }
    
}



- (void)pageChange
{
    [self.scrollView setContentOffset:CGPointMake((self.pageView.currentPage + 1) * VWidth , 0) animated:true];
    
}



//- (void)tapAction:(UITapGestureRecognizer *)tap
//{
//    UIScrollView *scrollView1 = (UIScrollView *)tap.view;
//    int x = scrollView1.contentOffset.x / self.frame.size.width - 1;
//    NSLog(@"%d",x);
//    MainHotSliderModel *slider = [self.sliderArr objectAtIndex:x];
//
//    [self.delegate HotSliderPassValue:slider];
//}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    int x = self.scrollView.contentOffset.x / self.scrollView.frame.size.width;
    self.pageView.currentPage = x;
    
    if (self.photoListArr.count != 0) {
        if (self.scrollView.contentOffset.x == 0) {
            [self.scrollView setContentOffset:CGPointMake((self.photoListArr.count - 2) * VWidth, 0)];
        }
        if (self.scrollView.contentOffset.x >= (self.photoListArr.count - 1) * VWidth) {
            
            /*如果是倒数第二张图片就跳 转到 第二张图片*/
            [self.scrollView setContentOffset:CGPointMake(VWidth, 0)];
        }
        
        self.pageView.currentPage = scrollView.contentOffset.x / VWidth - 1;
        [self.scrollView setContentOffset:CGPointMake((self.pageView.currentPage + 1) * VWidth, 0) animated:YES];
    }
    
    
    
    
    
}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.timer invalidate];
    self.timer = nil;
    NSLog(@"停止时间");
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    self.timer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(autoSlide) userInfo:nil repeats:YES];
    /* 创建 消息循环 */
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    /* 通过消息循环 把计时器的优先级提高到 与点击事件 一样 高*/
    [runLoop addTimer:self.timer forMode:NSRunLoopCommonModes];
    NSLog(@"开启时间");
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/






@end
